package com.wwt.server.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwt.server.mapper.EmployeeMapper;
import com.wwt.server.pojo.Employee;
import com.wwt.server.service.IEmployeeService;
import com.wwt.server.vo.Result;
import com.wwt.server.vo.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tao
 * @since 2021-12-05
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 条件获取所有员工
     * @param currentPage
     * @param size
     * @param
     * @param dates
     * @return
     */
    @Override
    public ResultPage getAllEmp(Integer currentPage, Integer size, Employee employee, LocalDate[] dates) {
        Page<Employee> page = new Page<>(currentPage,size);
        IPage<Employee> emp = employeeMapper.getAllEmp(page, employee, dates);
        return new ResultPage(emp.getTotal(),emp.getRecords());
    }

    @Override
    public Result getMaxId() {
        //Integer maxId = employeeMapper.getMaxId();
        QueryWrapper<Employee> wrapper = new QueryWrapper<Employee>().select("max(workId)");
        List<Map<String, Object>> list = employeeMapper.selectMaps(wrapper);
        Integer id = Integer.parseInt((String) list.get(0).get("max(workId)"));
        id ++;
        String format = String.format("%08d", id);
        return Result.success("查询成功！",format);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @Override
    public Result insertEmp(Employee employee) {
        //处理合同日期
        LocalDate begincontract = employee.getBegincontract();
        LocalDate endcontract = employee.getEndcontract();
        long contract = begincontract.until(endcontract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        double finalContract = Double.parseDouble(decimalFormat.format(contract));
        employee.setContractterm(finalContract);
        int i = employeeMapper.insert(employee);
        if (i == 1)
            return Result.success("插入成功!");
        return Result.fail("插入失败，请重新插入！");
    }

    @Override
    /**
     * 修改员工信息
     */
    public Result updateEmp(Employee employee) {
        LocalDate begincontract = employee.getBegincontract();
        LocalDate endcontract = employee.getEndcontract();
        long days = begincontract.until(endcontract, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        double contract = Double.parseDouble(decimalFormat.format(days / 365.00));
        employee.setContractterm(contract);
        int result = employeeMapper.updateById(employee);
        if (result == 1)
            return Result.success("更新成功！");
        return Result.fail("更新失败，请重试！");
    }

    /**
     * 获取导出Excel的数据
     * @param id
     * @return
     */
    @Override
    public List<Employee> getExportEmp(Integer id) {
        List<Employee> exportEmp = employeeMapper.getExportEmp(id);
        for(int i=0;i<exportEmp.size();i++){

            Employee employee = exportEmp.get(i);
            employee.setWorkage(countWorkAge(employee.getBegincontract()));
        }
        return exportEmp;
    }
    private Double countWorkAge(LocalDate date){
        LocalDate now = LocalDate.now();
        long days = date.until(now, ChronoUnit.DAYS);
        DecimalFormat decimalFormat = new DecimalFormat("##.00");
        return Double.parseDouble(decimalFormat.format(days/365.00));
    }
}
